package com.hf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hf.entity.Customer;
import com.hf.entity.Track;
import com.hf.service.ICustomerService;
import com.hf.service.ITrackService;
import com.hf.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author eight
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	ICustomerService iCustomerService;
	@Autowired
	ITrackService  iTrackService;
	/**
	 * 查询
	 * 
	 * @param cusName
	 * @param page
	 * @param limit
	 * @return http://localhost:8080/customer/getList?page=1&limit=10
	 */
	@RequestMapping("/getList")
	public Object getList(String cusName, Integer page, Integer limit) {
		IPage<Customer> customers = iCustomerService.queryCustomers(cusName, page, limit);
		List<Customer> list = customers.getRecords();// 获取当前的数据
		int total = (int) customers.getTotal();// 总条数
		return new Result(0, "查询成功", list, total);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public Object delete(Integer id) {
		if (iCustomerService.removeById(id)) {
			return new Result(0, "删除成功");
		}
		return new Result(1, "删除失败");
	}

	/**
	 * 修改
	 * 
	 * @param user
	 * @return http://localhost:8080/customer/update?loginName=zhangsan&id=1
	 */
	@RequestMapping("/update")
	public Object update(Customer cus) {
		if (iCustomerService.updateById(cus)) {
			return new Result(0, "修改成功");
		}
		return new Result(1, "修改失败");
	}

	/**
	 * 添加
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("/insert")
	public Object insert(Customer cus) {
		if (iCustomerService.save(cus)) {
			return new Result(0, "添加成功");
		}
		return new Result(1, "添加失败");
	}

	/**
	 * 根据客户id找到跟踪记录一或多条
	 * 
	 * @param cusId
	 * @return
	 */
	@RequestMapping("/getTrackList")
	public Object getTrackList(Integer cusId) {
		 List<Track> list = iTrackService.getList(cusId);
		return new Result(0,"查询成功",list,10);

	}
}
